/*
 * ResourceCollection.h
 *
 *  Created on: Jun 8, 2010
 *      Author: Ry
 */

#ifndef RESOURCECOLLECTION_H_
#define RESOURCECOLLECTION_H_

#include <string>
#include <vector>

#include "world/Object.h"
#include "art/ShaderProgram.h"
#include "texture/Texture.h"
#include "mesh/Material.h"

class MeshLoader;

/**
 * Manages resources used by the game.  All resources created by the resource
 * collection will be freed when the collection is deleted.
 */
class ResourceCollection {
private:
	Material* defaultMaterial;

protected:
	std::vector<Object*> objects;
	std::vector<ShaderProgram*> programs;
	std::vector<Texture*> textures;
	std::vector<Material*> materials;
	std::vector<Mesh*> meshes;
	std::vector<MeshLoader*> meshLoaders;
public:
	ResourceCollection();

	/**
	 * Create a new object.
	 */
	Object* newObject();

	/**
	 * Create a new shader program.
	 */
	ShaderProgram* newShaderProgram();

	/**
	 * Creates a new material.
	 */
	Material* newMaterial();

	/**
	 * @return Returns a pointer to a basic material
	 */
	Material* getDefaultMaterial() {
		return defaultMaterial;
	}

	/**
	 * Adds a material to the collection.
	 */
	void put(Material* material);

	/**
	 * @return Returns true if the material exists in the collection.
	 */
	bool exists (Material* material);

	/**
	 * Loads a texture
	 */
	Texture* loadTexture(std::string file, bool mipmaps);

	/**
	 * Load a new mesh from disk.
	 */
	Mesh* loadMesh(std::string file, ShaderProgram *program);

	/**
	 * Adds a mesh to the collection.
	 */
	void put(Mesh* mesh);

	/**
	 * Updates all meshes.  Data is resent to graphics hardware.
	 *
	 * Should be called when a new GL context is created.
	 */
	void updateMeshes();

	/**
	 * Reloads all textures.
	 *
	 * Should be called when a new GL context is created.
	 */
	void updateTextures();

	/**
	 * Rebuilds all the shader programs.
	 */
	void rebuildShaderPrograms();

	virtual ~ResourceCollection();
};

#endif /* RESOURCECOLLECTION_H_ */
